package com.daviteq.fw.controller;

import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.daviteq.fw.controller.form.LoginForm;
import com.daviteq.fw.service.AccountService;

@Controller
public class LoginController {
    @Autowired
    private AccountService accountService;

    @RequestMapping(method = RequestMethod.GET, value = "/")
    public String showForm(ModelMap map) {
        return "login";
    }

    @RequestMapping(method = RequestMethod.POST, value = "doLogin")
    public String doLogin(@Valid LoginForm loginForm, BindingResult result, Map<String, Object> model) {
        if (result.hasErrors()) {
            return "login";
        }
        return accountService.isAuthenticated(loginForm) ? "login" : "common/test";
    }

    @RequestMapping(method = RequestMethod.POST, value = "resendPassword")
    public String resendPassword(@Valid LoginForm loginForm, BindingResult result, Map<String, Object> model) {
        if (result.hasErrors()) {
            return "login";
        }
        return "success";
    }
}
